package com.bosssoft.service_order.controller;

import com.bosssoft.service_order.common.api.CommonPage;
import com.bosssoft.service_order.common.api.CommonResult;
import com.bosssoft.service_order.domain.dto.OrderDetail;
import com.bosssoft.service_order.domain.dto.ProtalOrderQueryParam;
import com.bosssoft.service_order.service.PortalOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 订单管理Controller
 */
@Controller
@Api(tags = "PortalOrderController", description = "订单管理")
@RequestMapping("/portalOrder")
public class PortalOrderController {
    @Autowired
    private PortalOrderService portalOrderService;

    @ApiOperation("根据购物车信息生成订单")
    @RequestMapping(value = "/generateOrder", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult generateOrder(@RequestBody ProtalOrderQueryParam protalOrderQueryParam) {
        int result = portalOrderService.createOrder(protalOrderQueryParam);
        return CommonResult.success(result, "下单成功");
    }

    @ApiOperation("按状态分页获取用户订单列表")
    @ApiImplicitParam(name = "status", value = "订单状态：-1->全部；0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭",
            defaultValue = "-1", allowableValues = "-1,0,1,2,3,4", paramType = "query", dataType = "int")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<OrderDetail>> list(@ApiParam(value = "用户ID") Long uid,
                                                      @ApiParam(value = "订单状态") Integer status,
                                                      @ApiParam(value = "页码") Integer pageNum,
                                                      @ApiParam(value = "大小") Integer pageSize) {
        CommonPage<OrderDetail> orderPage = portalOrderService.list(uid, status,pageNum,pageSize);
        return CommonResult.success(orderPage);
    }

    @ApiOperation("根据ID获取订单详情")
    @RequestMapping(value = "/detail/{orderId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<OrderDetail> detail(@ApiParam(value = "订单ID")@PathVariable Long orderId) {
        OrderDetail orderDetail = portalOrderService.detail(orderId);
        return CommonResult.success(orderDetail);
    }

    @ApiOperation("用户支付成功的回调")
    @RequestMapping(value = "/paySuccess", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult paySuccess(@RequestParam Long orderId,@RequestParam Integer payType) {
        Integer count = portalOrderService.paySuccess(orderId,payType);
        return CommonResult.success(count, "支付成功");
    }

}
